<?php

namespace app\common\validate;

use app\common\service\StatusService;
use think\Validate;

class Cart extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
    	'goods_id' => 'require|gt:0',
	    'number' => 'require|gt:0',
	    'spec_id' => 'checkSpec'
    ];
    /**
     * 提示消息
     */
    protected $message = [
    	'goods_id' => '商品ID不能为空',
	    'number' => '数量最少为1',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
    
    protected function checkSpec($value, $rule = '', $data) {
    	$model = new \app\common\model\GoodsSpec();
    	$specList = $model->getSpecsByGoodsId($data['goods_id'], StatusService::STATUS_NORMAL);
    	if(!$value && $specList) {
    		return '请选择规格';
	    }
    	
    	return true;
    }
}
